// RuleJudge.Judge.LRTIX
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using System.Windows.Forms;
using RuleJudge.Judge;
using RuleJudge.RuleData;
using SLANCommon.Enum;

public class LRTIX : BaseJudge
{
    public LRTIX()
    {
        Type = EProjectType.AQ;
        base.TubesNo = new List<int> { 1, 1, 1, 1, 2, 2, 2, 2 };
        base.ChannelsNo = new List<int> { 1, 2, 3, 4, 1, 2, 3, 4 };
    }

    int FAM = 0;
    int VIC = 1;
    int ROX = 2;
    int CY5 = 3;

    bool NC_Check_Pass = false;
    bool PC_Check_Pass = false;
    bool Leak_NC_Control = false;
    bool Leak_PC_Control = false;

    // 阴性质控阈值(因为SLAN软件默认只显示2位小数，当Ct值小数位小于0.01，会四舍五入舍去，例如33.0049会显示为33.00，造成误以为插件判读出错)
    const double SPN_Cutoff = 32.01;  // 肺炎链球菌（SPN）
    const double HI_Cutoff = 32.01;   // 流感嗜血杆菌（HI）
    const double PA_Cutoff = 32.01;   // 铜绿假单胞菌(PA)
    const double AB_Cutoff = 33.01;   // 鲍曼不动杆菌(AB)
    const double KP_Cutoff = 32.01;   // 肺炎克雷伯菌（KP）
    const double SA_Cutoff = 33.01;   // 金黄色葡萄球菌（SA）
    const double IC_Cutoff = 35.01;   // 内标IC-A, IC-B, IC-C
    const double PC_Cutoff = 30.01;   // 阳性质控PC


    // 反应管各通道检测靶标
    //      0         1        2       3
    //  |------------------------------------
    //  |   FAM  |   VIC  |   ROX  |   CY5  |
    //  |   SPN  |   HI   |   PA   |   IC-A |
    //  |-----------------------------------|
    //  |   FAM  |   VIC  |   ROX  |   CY5  |
    //  |   AB   |   KP   |   SA   |   IC-B |
    //  |-----------------------------------|

    // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
    // 开始因阳性质控、待测(未知)样本判读
    public override string Judge(List<Sample> samples)
    {
        if (samples.Count == 0)
        {
            return "无判断样本！";
        }

        #region 阴性指控判断
        List<Sample> NC_list = samples.Where((Sample s) => s.Type == SampleType.Negative).ToList();
        if (NC_list.Count != 0)
        {
            foreach (Sample NC in NC_list)
            {
                NC_Check_Pass = ((NC.Tubes[0].Targets[FAM].Result.Ct >= SPN_Cutoff && NC.Tubes[0].Targets[VIC].Result.Ct >= HI_Cutoff && NC.Tubes[0].Targets[ROX].Result.Ct >= PA_Cutoff && NC.Tubes[0].Targets[CY5].Result.Ct < IC_Cutoff) &&
                                 (NC.Tubes[1].Targets[FAM].Result.Ct >= AB_Cutoff  && NC.Tubes[1].Targets[VIC].Result.Ct >= KP_Cutoff && NC.Tubes[1].Targets[ROX].Result.Ct >= SA_Cutoff && NC.Tubes[1].Targets[CY5].Result.Ct < IC_Cutoff));
                NC.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = NC_Check_Pass ? "阴性质控合格" : "阴性质控不合格",
                    Color = NC_Check_Pass ? Colors.Green : Colors.Red,
                    BackgroundColor = NC_Check_Pass ? Colors.LightGreen : Colors.LightYellow,
                    ForegroundColor = NC_Check_Pass ? Colors.Red : Colors.Blue,
                });
                NC.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = NC_Check_Pass ? "阴性质控合格" : "阴性质控不合格",
                    Color = NC_Check_Pass ? Colors.Green : Colors.Red,
                    BackgroundColor = NC_Check_Pass ? Colors.LightGreen : Colors.LightYellow,
                    ForegroundColor = NC_Check_Pass ? Colors.Red : Colors.Blue,
                });
            }
        }
        else
        {
            Leak_NC_Control = true;
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 阳性质控判读
        List<Sample> PC_list = samples.Where((Sample s) => s.Type == SampleType.HighPositive).ToList();
        if (PC_list.Count != 0)
        {
            foreach (Sample PC in PC_list)
            {
                PC_Check_Pass = ((PC.Tubes[0].Targets[FAM].Result.Ct < PC_Cutoff && PC.Tubes[0].Targets[VIC].Result.Ct < PC_Cutoff && PC.Tubes[0].Targets[ROX].Result.Ct < PC_Cutoff) &&
                                 (PC.Tubes[1].Targets[FAM].Result.Ct < PC_Cutoff && PC.Tubes[1].Targets[VIC].Result.Ct < PC_Cutoff && PC.Tubes[1].Targets[ROX].Result.Ct < PC_Cutoff));
                PC.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = PC_Check_Pass ? "阳性质控合格" : "阳性质控不合格",
                    Color = PC_Check_Pass ? Colors.Green : Colors.Red,
                    BackgroundColor = PC_Check_Pass ? Colors.LightGreen : Colors.LightYellow,
                    ForegroundColor = PC_Check_Pass ? Colors.Red : Colors.Blue,
                });
                PC.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = PC_Check_Pass ? "阳性质控合格" : "阳性质控不合格",
                    Color = PC_Check_Pass ? Colors.Green : Colors.Red,
                    BackgroundColor = PC_Check_Pass ? Colors.LightGreen : Colors.LightYellow,
                    ForegroundColor = PC_Check_Pass ? Colors.Red : Colors.Blue,
                });
            }
        }
        else
        {
            Leak_PC_Control = true;
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 待测样本判读
        List<Sample> UnknownSample_list = samples.Where((Sample s) => s.Type == SampleType.Unknown).ToList();
        foreach (Sample Unknown_Sample in UnknownSample_list)
        {
            // 样本内参是否合格:（1）反应孔有非内参靶标阳性；（2）反应孔靶标均为阴性，且内参阳性。
            bool IC_Check_Pass = false;
            if ((Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct < SPN_Cutoff || Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct < HI_Cutoff || Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct < PA_Cutoff || Unknown_Sample.Tubes[0].Targets[CY5].Result.Ct < IC_Cutoff) &&
                (Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct < AB_Cutoff  || Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct < KP_Cutoff || Unknown_Sample.Tubes[1].Targets[ROX].Result.Ct < SA_Cutoff || Unknown_Sample.Tubes[1].Targets[CY5].Result.Ct < IC_Cutoff))
            {
                IC_Check_Pass = true;
            }
            if (!IC_Check_Pass)
            {
                Unknown_Sample.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，需重新检测！",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                    ForegroundColor = Colors.LightCoral,
                });
                Unknown_Sample.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，需重新检测！",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                    ForegroundColor = Colors.LightCoral,
                });
                continue;
            }
            // 反应管A孔FAM通道：肺炎链球菌（SPN）
            Unknown_Sample.Tubes[0].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct < SPN_Cutoff ? "肺炎链球菌阳性" : "肺炎链球菌阴性",
                Color = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct < SPN_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct < SPN_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管A孔VIC通道：流感嗜血杆菌（HI）
            Unknown_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct < HI_Cutoff ? "流感嗜血杆菌阳性" : "流感嗜血杆菌阴性",
                Color = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct < HI_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct < HI_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管A孔ROX通道：铜绿假单胞菌(PA)
            Unknown_Sample.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct < PA_Cutoff ? "铜绿假单胞菌阳性" : "铜绿假单胞菌阴性",
                Color = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct < PA_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct < PA_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // *****************************************************************************************************************************
            // 反应管B孔FAM通道：鲍曼不动杆菌(AB)
            Unknown_Sample.Tubes[1].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct < AB_Cutoff ? "鲍曼不动杆菌阳性" : "鲍曼不动杆菌阴性",
                Color = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct < AB_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct < AB_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管B孔VIC通道：肺炎克雷伯菌（KP）
            Unknown_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct < KP_Cutoff ? "肺炎克雷伯菌阳性" : "肺炎克雷伯菌阴性",
                Color = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct < KP_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct < KP_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管B孔ROX通道：金黄色葡萄球菌（SA）
            Unknown_Sample.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[ROX].Result.Ct < SA_Cutoff ? "金黄色葡萄球菌阳性" : "金黄色葡萄球菌阴性",
                Color = Unknown_Sample.Tubes[1].Targets[ROX].Result.Ct < SA_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[ROX].Result.Ct < SA_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 重测样本判读
        List<Sample> RetestSample_list = samples.Where((Sample s) => s.Type == SampleType.RetestSample).ToList();
        foreach (Sample Retest_Sample in RetestSample_list)
        {
            // 样本内参是否合格:（1）反应孔有非内参靶标阳性；（2）反应孔靶标均为阴性，且内参阳性。
            bool IC_Check_Pass = false;
            if ((Retest_Sample.Tubes[0].Targets[FAM].Result.Ct < SPN_Cutoff || Retest_Sample.Tubes[0].Targets[VIC].Result.Ct < HI_Cutoff || Retest_Sample.Tubes[0].Targets[ROX].Result.Ct < PA_Cutoff || Retest_Sample.Tubes[0].Targets[CY5].Result.Ct < IC_Cutoff) &&
                (Retest_Sample.Tubes[1].Targets[FAM].Result.Ct < AB_Cutoff  || Retest_Sample.Tubes[1].Targets[VIC].Result.Ct < KP_Cutoff || Retest_Sample.Tubes[1].Targets[ROX].Result.Ct < SA_Cutoff || Retest_Sample.Tubes[1].Targets[CY5].Result.Ct < IC_Cutoff))
            {
                IC_Check_Pass = true;
            }
            if (!IC_Check_Pass)
            {
                Retest_Sample.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，需重新检测！",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                    ForegroundColor = Colors.LightCoral,
                });
                Retest_Sample.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，需重新检测！",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                    ForegroundColor = Colors.LightCoral,
                });
                continue;
            }
            // 反应管A孔FAM通道：肺炎链球菌（SPN）
            Retest_Sample.Tubes[0].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct < SPN_Cutoff ? "肺炎链球菌阳性" : "肺炎链球菌阴性",
                Color = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct < SPN_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct < SPN_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管A孔VIC通道：流感嗜血杆菌（HI）
            Retest_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct < HI_Cutoff ? "流感嗜血杆菌阳性" : "流感嗜血杆菌阴性",
                Color = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct < HI_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct < HI_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管A孔ROX通道：铜绿假单胞菌(PA)
            Retest_Sample.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct < PA_Cutoff ? "铜绿假单胞菌阳性" : "铜绿假单胞菌阴性",
                Color = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct < PA_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct < PA_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // *****************************************************************************************************************************
            // 反应管B孔FAM通道：鲍曼不动杆菌(AB)
            Retest_Sample.Tubes[1].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct < AB_Cutoff ? "鲍曼不动杆菌阳性" : "鲍曼不动杆菌阴性",
                Color = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct < AB_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct < AB_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管B孔VIC通道：肺炎克雷伯菌（KP）
            Retest_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct < KP_Cutoff ? "肺炎克雷伯菌阳性" : "肺炎克雷伯菌阴性",
                Color = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct < KP_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct < KP_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管B孔ROX通道：金黄色葡萄球菌（SA）
            Retest_Sample.Tubes[1].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[ROX].Result.Ct < SA_Cutoff ? "金黄色葡萄球菌阳性" : "金黄色葡萄球菌阴性",
                Color = Retest_Sample.Tubes[1].Targets[ROX].Result.Ct < SA_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[ROX].Result.Ct < SA_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
        }
        #endregion

        /* *****************************************************************************************************
        * 实验中未设置阴阳性质控不影响判读，仅给出弹窗提醒缺少质控 *************************************************
        * 阴阳性质控不合格的，则给出提醒，但仍然进行判读 **********************************************************
        * *****************************************************************************************************
        */
        #region 质控结果异常（不合格，缺失）提示
        if (!NC_Check_Pass || !PC_Check_Pass)
        {
            System.Windows.Forms.MessageBox.Show("一个或多个阴阳性质控不符合要求！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        if (Leak_NC_Control || Leak_PC_Control)
        {
            System.Windows.Forms.MessageBox.Show("实验缺少阴性质控或阳性质控！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        #endregion

        return string.Empty;
    }
}